﻿namespace Sitecore.ChangesTracker.Commands
{
  using System;
  using System.Collections.Generic;
  using System.Collections.Specialized;
  using System.Linq;
  using System.Text;
  using Sitecore.Data.Archiving;
  using Sitecore.Diagnostics;
  using Sitecore.Globalization;
  using Sitecore.Shell.Framework.Commands;
  using Sitecore.Text;
  using Sitecore.Web.UI.Sheer;
  using Sitecore.Web.UI.WebControls;
  using Sitecore.Web.UI.XamlSharp.Continuations;


  public class Delete : Command, ISupportsContinuation
  {
    public override void Execute(CommandContext context)
    {
      Assert.ArgumentNotNull(context, "context");
      string str = context.Parameters["item"];
      if (string.IsNullOrEmpty(str))
      {
        SheerResponse.Alert("Select an item first.", new string[0]);
      }
      else
      {
        NameValueCollection parameters = new NameValueCollection();
        parameters["item"] = str;
        parameters["archivename"] = context.Parameters["archivename"];
        ClientPipelineArgs args = new ClientPipelineArgs(parameters);
        ContinuationManager.Current.Start(this, "Run", args);
      } 
    }
    protected void Run(ClientPipelineArgs args)
    {
      Assert.ArgumentNotNull(args, "args");
      if (args.IsPostBack)
      {
        if (args.Result == "yes")
        {
          Sitecore.Data.Archiving.Archive archive = ArchiveManager.Providers["changetracker"].GetArchive(args.Parameters["archivename"], Context.ContentDatabase);
          Assert.IsNotNull(archive, typeof(Shell.Framework.Commands.Archive));
          ListString str = new ListString(args.Parameters["item"]);
          foreach (string str2 in str)
          {
            Guid guid = new Guid(str2);
            ArchiveQuery query = new ArchiveQuery
            {
              ArchivalId = guid
            };
            archive.RemoveEntries(query);
            Log.Audit(this, "Delete from archive: archive: {0}, id: {1}", new string[] { archive.Name, guid.ToString() });
          }
          SheerResponse.SetLocation(string.Empty);
        }
      }
      else
      {
        ListString str3 = new ListString(args.Parameters["item"]);
        if (str3.Count == 1)
        {
          Sitecore.Data.Archiving.Archive archive2 = ArchiveManager.Providers["changetracker"].GetArchive(args.Parameters["archivename"], Context.ContentDatabase);
          Assert.IsNotNull(archive2, typeof(Shell.Framework.Commands.Archive));
          ArchiveQuery query2 = new ArchiveQuery
          {
            ArchivalId = new Guid(str3[0])
          };
          ArchiveEntry entry = archive2.GetEntries(query2, 0, 1).FirstOrDefault<ArchiveEntry>();
          if (entry == null)
          {
            SheerResponse.Alert("Item not found.", new string[0]);
            return;
          }
          string name = entry.Name;
          SheerResponse.Confirm(Translate.Text("Are you sure you want to permanently delete audit point \"{0}\"?", new object[] { name }));
        }
        else
        {
          SheerResponse.Confirm(Translate.Text("Are you sure you want to permanently delete these audit points: {0}?", new object[] { str3.Count }));
        }
        args.WaitForPostBack();
      }
    }

 

 

  }
}
